﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web.UI.WebControls;
using SalesAndStoreApp.BLL;
using SalesAndStoreApp.DAL.DAO;

namespace SalesAndStoreApp.UI.StoreManager
{
    public partial class CurrentStockReportUI : System.Web.UI.Page
    {
        private ProductManager productManager = new ProductManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetNoStore();
            if (!IsPostBack)
            {
                try
                {
                    currentdateLabel.Text = DateTime.Now.ToShortDateString();
                    currentTimeLabel.Text = DateTime.Now.TimeOfDay.ToString();
                    List<Product> products = productManager.GetAll();
                    List<Product> productSource = products.Select(product => productManager.GetById(product.Id)).ToList();
                    var dt = new DataTable();

                    DataColumn productColumn = new DataColumn("Product", typeof(String));
                    dt.Columns.Add(productColumn);

                    DataColumn currentStockQty = new DataColumn("CurrentStockQty", typeof(System.Int32));
                    dt.Columns.Add(currentStockQty);

                    DataColumn reOrderColumn = new DataColumn("ReOrderLevel", typeof(System.Int32));
                    dt.Columns.Add(reOrderColumn);

                    foreach (var product in productSource)
                    {
                        DataRow drow = dt.NewRow();
                        drow[productColumn] = product.Name;
                        drow[currentStockQty] = product.Inventory.Quantity;
                        drow[reOrderColumn] = product.Inventory.ReOrderLevel;
                        dt.Rows.Add(drow);
                    }

                    foreach (DataColumn col in dt.Columns)
                    {
                        BoundField boundField = new BoundField();
                        boundField.DataField = col.ColumnName;
                        boundField.HeaderText = col.ColumnName;
                        currentStockGridView.Columns.Add(boundField);
                    }
                    currentStockGridView.DataSource = dt;
                    currentStockGridView.DataBind();
                    int i = 0;
                    foreach (var product in productSource)
                    {

                        if (product.Inventory.Quantity < product.Inventory.ReOrderLevel)
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                currentStockGridView.Rows[i].Cells[j].BackColor = Color.Red;
                                currentStockGridView.Rows[i].Cells[j].ForeColor = Color.White;
                            }
                            currentStockGridView.DataBind();
                        }
                        i = i + 1;
                    }
                }
                catch (Exception exception)
                {
                    string message = "An error occurred!  In details - ";
                    string footerMessage = " (if you dont understand the error message please contact with the vendor)";
                    Response.Redirect("~/UI/ErrorPage.aspx?Error="+message + @""""+exception.Message +@""""+ footerMessage );
                }
            } 

        }
    }
}